<?php
/**
 *Navigation Model
 *fetches and prepare data for the navigation
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 *Class Navigation_Model
 *
 *@package model
 */
class Navigation_Model extends Model{

    /**
     * Holds the database reference
     * @var object
     */
    protected $oDBInstance;
    /**
     * Model constructor
     * Establishes connection to database
     */

    public function __construct(){

        /**
         * Gets the database reference from Mysql_dbconnection
         * @var object
         */
        $this->oDBInstance = Mysql_dbconnection::getInstance();

    }

    /**
     * Returns the page structure
     * @param integer page id of the current page
     * @param integer language of the title the that should be returned
     * @param string page typ condition
     */
    public function getPageStructure($iPid, $iLanguage, $sPageTypCondition= ''){
        $aPageStructure = array();
        //Gets sub pages
        $aPageStructure = $this->getSubPages($iPid, $iLanguage, $sPageTypCondition);

        //Creates an array which represents the page structure/navigation
        if (!(empty($aPageStructure))) {
            for ($i =0; $i< sizeof($aPageStructure); $i++){
                $aPage = &$aPageStructure[$i];
                $aPage['subPages'] = $this->getPageStructure($aPage['id'], $iLanguage, $sPageTypCondition);

            }
        }
        return $aPageStructure;

    }
    /**
     * Returns the sub pages
     * @param integer page id of the current page
     * @param integer language of the title the that should be returned
     * @param string page typ condition
     */
    public function getSubPages($iPid, $iLanguage, $sPageTypCondition = ''){
        if($sPageTypCondition != ''){
            $sPageTypCondition = "AND $sPageTypCondition";
        }
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name, position, link, parent, level
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND cms_only = 0 AND lid = $iLanguage $sPageTypCondition) as a");
        $this->oDBInstance->_setDefaultColumn('id');

        $iParentID = $this->oDBInstance->getSQLValueString($iPid,'int');
        if ($iPid == 'NULL'){
            $this->oDBInstance->performSelect('id, name, link, level', '', "a.parent IS NULL ORDER BY position");
        }else {
            $this->oDBInstance->performSelect('id, name, link, level', '', "a.parent = '$iParentID' ORDER BY position");
        }
        return $this->oDBInstance->fetch('array');
    }
    /**
     * Returns the pages by page type content / product
     * @param integer page id of the current page
     * @param integer language of the title the that should be returned
     * @param string page type product / content
     */
    public function getPagesByTyp($iPid = '', $iLanguage, $sTyp = ''){
        $iParentID = $this->oDBInstance->getSQLValueString($iPid,'int');
        //If $Pid empty fetch all products
        if($iPid == ''){
            $this->oDBInstance->query("SELECT id, name, position, link, parent, level
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND cms_only = 0 AND lid = $iLanguage AND type = '$sTyp' ORDER BY parent, position");
        }else{
            $this->oDBInstance->query("SELECT id, name, position, link, parent, level
            	  FROM (SELECT * FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND cms_only = 0 AND lid = $iLanguage AND type = '$sTyp') as p
            	  WHERE parent = '$iParentID' ORDER BY paretn, position");
        }

        $aProducts = $this->oDBInstance->fetch('array');

        return $aProducts;

    }


}
?>